allow reregistering but check for differences
authorRupert Weber <gimp@leguanease.org>
Fri, 1 Oct 2010 00:18:53 +0000 (02:18 +0200)
committerØyvind Kolås <pippin@gimp.org>
Thu, 20 Jan 2011 21:42:59 +0000 (21:42 +0000)
babl_model_new() emitted a warning when reregistering a model,
babl_format_new(), babl_type_new(), and babl_component_new()
quietly returned a preexisting object.

Changed so that all four functions return the preexisting object
if the new registration doesn't conflict, otherwise make the
error fatal.

(Fixed some conflicting and redundant registrations for
"Y'CbCr u8" along the way)

babl/babl-component.c
babl/babl-format.c
babl/babl-model.c
babl/babl-type.c
babl/base/formats.c
babl/base/model-ycbcr.c
extensions/gggl-lies.c
extensions/gggl.c

index 54a06b3fe83b8133f6b238738eb93d25562c7126..0d036474ff63cb672ab3e6ce40988c758c984de0 100644 (file)
@@ -44,6 +44,19 @@ component_new (const char *name,
   return babl;
 }
 
+
+static int
+is_component_duplicate (Babl *babl, int luma, int chroma, int alpha)
+{
+  if (babl->component.luma   != luma   ||
+      babl->component.chroma != chroma ||
+      babl->component.alpha  != alpha)
+    return 0;
+
+  return 1;
+}
+
+
 Babl *
 babl_component_new (void *first_arg,
                     ...)
@@ -54,7 +67,8 @@ babl_component_new (void *first_arg,
   int         luma   = 0;
   int         chroma = 0;
   int         alpha  = 0;
-  const char *arg    = (char *) first_arg;
+  const char *name   = first_arg;
+  const char *arg;
 
   va_start (varg, first_arg);
 
@@ -95,22 +109,29 @@ babl_component_new (void *first_arg,
 
       else
         {
-          babl_fatal ("unhandled argument '%s' for format '%s'", arg, first_arg);
+          babl_fatal ("unhandled argument '%s' for component '%s'", arg, name);
         }
     }
 
   va_end (varg);
 
-  babl = babl_db_exist (db, id, first_arg);
+  babl = babl_db_exist (db, id, name);
+  if (id && !babl && babl_db_exist (db, 0, name))
+    babl_fatal ("Trying to reregister BablComponent '%s' with different id!",
+                name);
+
   if (babl)
     {
       /* There is an instance already registered by the required id/name,
-       * returning the preexistent one instead.
+       * returning the preexistent one instead if it doesn't differ.
        */
+      if (!is_component_duplicate (babl, luma, chroma, alpha))
+        babl_fatal ("BablComponent '%s' already registered "
+                    "with different attributes!", name);
       return babl;
     }
 
-  babl = component_new (first_arg, id, luma, chroma, alpha);
+  babl = component_new (name, id, luma, chroma, alpha);
 
   /* Since there is not an already registered instance by the required
    * id/name, inserting newly created class into database.
index 186d15a3e5e387a513549853bd6238b0446672d6..3760452a65948e570e280009c500cba597c0cec3 100644 (file)
@@ -51,7 +51,7 @@ format_new (const char     *name,
 {
   Babl *babl;
 
-  /* i is desintation position */
+  /* i is destination position */
   int i, j, component_found = 0;
   for (i = 0; i < model->components; i++)
     {
@@ -236,6 +236,32 @@ babl_format_n (Babl *btype,
   return babl;
 }
 
+static int
+is_format_duplicate (Babl           *babl,
+                     int             planar,
+                     int             components,
+                     BablModel      *model,
+                     BablComponent **component,
+                     BablSampling  **sampling,
+                     BablType      **type)
+{
+  int i;
+
+  if (babl->format.planar     != planar     ||
+      babl->format.components != components ||
+      babl->format.model      != model)
+    return 0;
+
+  for (i = 0; i < components; i++)
+    {
+      if (babl->format.component[i] != component[i] ||
+          babl->format.sampling[i]  != sampling[i]  ||
+          babl->format.type[i]      != type[i])
+        return 0;
+    }
+  return 1;
+}
+
 Babl *
 babl_format_new (void *first_arg,
                  ...)
@@ -359,11 +385,19 @@ babl_format_new (void *first_arg,
     name = create_name (model, components, component, type);
 
   babl = babl_db_exist (db, id, name);
+  if (id && !babl && babl_db_exist (db, 0, name))
+    babl_fatal ("Trying to reregister BablFormat '%s' with different id!", name);
+
   if (babl)
     {
       /* There is an instance already registered by the required id/name,
-       * returning the preexistent one instead.
+       * returning the preexistent one instead if it doesn't differ.
        */
+      if (!is_format_duplicate (babl, planar, components, model,
+                                component, sampling, type))
+        babl_fatal ("BablFormat '%s' already registered "
+                    "with different content!", name);
+
       babl_free (name);
       return babl;
     }
index 4766b3b4c1233f824ead2c2dcd72d9f18de273c8..11a3dd1c3b43c98b856078745947a378a5ee13db 100644 (file)
@@ -74,6 +74,24 @@ model_new (const char     *name,
   return babl;
 }
 
+static int
+is_model_duplicate (Babl *babl, int components, BablComponent **component)
+{
+  int   i;
+
+  if (babl->model.components != components)
+    return 0;
+
+  for (i = 0; i < components; i++)
+    {
+      if (babl->model.component[i] != component[i])
+        return 0;
+    }
+
+  return 1;
+}
+
+
 Babl *
 babl_model_new (void *first_argument,
                 ...)
@@ -166,6 +184,8 @@ babl_model_new (void *first_argument,
     name = babl_model_create_name (components, component);
 
   babl = babl_db_exist (db, id, name);
+  if (id && !babl && babl_db_exist (db, 0, name))
+    babl_fatal ("Trying to reregister BablModel '%s' with different id!", name);
 
   if (! babl)
     {
@@ -175,7 +195,9 @@ babl_model_new (void *first_argument,
     }
   else
     {
-      babl_log ("Warning: BablModel '%s' already registered!", name);
+      if (!is_model_duplicate (babl, components, component))
+        babl_fatal ("BablModel '%s' already registered "
+                    "with different components!", name);
     }
 
   babl_free (name);
index 467b33ff17df4c17a12fd04e56a9b0f8357769ab..1bf7a0754d37a84beb8c7d4a5c0b9fad4fe7a918 100644 (file)
@@ -55,6 +55,15 @@ type_new (const char *name,
   return babl;
 }
 
+static int
+is_type_duplicate (Babl *babl, int bits)
+{
+  if (babl->type.bits != bits)
+    return 0;
+
+  return 1;
+}
+
 Babl *
 babl_type_new (void *first_arg,
                ...)
@@ -68,8 +77,8 @@ babl_type_new (void *first_arg,
   long        max        = 255;
   double      min_val    = 0.0;
   double      max_val    = 0.0;
-
-  const char *arg = first_arg;
+  const char *name = first_arg;
+  const char *arg;
 
   va_start (varg, first_arg);
 
@@ -122,22 +131,30 @@ babl_type_new (void *first_arg,
 
       else
         {
-          babl_fatal ("unhandled argument '%s' for format '%s'", arg, first_arg);
+          babl_fatal ("unhandled argument '%s' for format '%s'", arg, name);
         }
     }
 
   va_end (varg);
 
-  babl = babl_db_exist (db, id, first_arg);
+  babl = babl_db_exist (db, id, name);
+  if (id && !babl && babl_db_exist (db, 0, name))
+    babl_fatal ("Trying to reregister BablType '%s' with different id!", name);
+
   if (babl)
     {
       /* There is an instance already registered by the required id/name,
-       * returning the preexistent one instead.
+       * returning the preexistent one instead if it doesn't differ.
        */
+
+      if (!is_type_duplicate (babl, bits))
+        babl_fatal ("BablType '%s' already registered "
+                    "as different type!", name);
+
       return babl;
     }
 
-  babl = type_new (first_arg, id, bits);
+  babl = type_new (name, id, bits);
 
   /* Since there is not an already registered instance by the required
    * id/name, inserting newly created class into database.
index 5e6e0c0718f879085bd5a9f4cb9e1200765a6169..65b3d77424c959eb8c37308f0f49f80ff5d192ba 100644 (file)
@@ -155,13 +155,21 @@ babl_formats_init (void)
     babl_type_from_id (BABL_U8),
     babl_component_from_id (BABL_LUMA),
     NULL);
+
+  /* overriding name, since the generated name would be wrong due
+   * to differing types
+   */
   babl_format_new (
     "name", "Y'CbCr u8",
+    "planar",
     babl_model_from_id (BABL_YCBCR),
     babl_type_from_id (BABL_U8_LUMA),
+    babl_sampling (1, 1),
     babl_component_from_id (BABL_LUMA),
     babl_type_from_id (BABL_U8_CHROMA),
+    babl_sampling (2, 2),
     babl_component_from_id (BABL_CB),
+    babl_sampling (2, 2),
     babl_component_from_id (BABL_CR),
     NULL);
   babl_format_new (
@@ -310,18 +318,6 @@ babl_formats_init (void)
     babl_component_from_id (BABL_LUMINANCE),
     NULL);
 
-  /* overriding name, since the generated name would be wrong due
-   * to differing types
-   */
-  babl_format_new (
-    "name", "Y'CbCr u8",
-    babl_model_from_id (BABL_YCBCR),
-    babl_type_from_id (BABL_U8_LUMA),
-    babl_component_from_id (BABL_LUMA),
-    babl_type_from_id (BABL_U8_CHROMA),
-    babl_component_from_id (BABL_CB),
-    babl_component_from_id (BABL_CR),
-    NULL);
   babl_format_new (
     babl_model_from_id (BABL_YCBCR),
     babl_type_from_id (BABL_FLOAT),
index c4f6ab95d9d9077953f1ac4b44fc6a0e00ab18ef..13f35818ecf0f29d168406f8dc3cce1d57a133c7 100644 (file)
@@ -242,7 +242,6 @@ formats (void)
 {
   babl_format_new (
     "name", "Y'CbCr u8",
-    "id", BABL_YCBCR420,
     "planar",
     babl_model_from_id (BABL_YCBCR),
     babl_type_from_id (BABL_U8_LUMA),
index b93d47c5c7dde31613b686bea9c25ebe8ce9b0ed..33d19efc7a9c36371ea6a28c1b04005c1fd4261e 100644 (file)
@@ -1926,10 +1926,13 @@ init (void)
     NULL);
   Babl *yuv8 = babl_format_new (
     "name", "Y'CbCr u8",
+    "planar",
     babl_model ("Y'CbCr"),
     babl_type ("u8-luma"),
+    babl_sampling (1, 1),
     babl_component ("Y'"),
     babl_type ("u8-chroma"),
+    babl_sampling (2, 2),
     babl_component ("Cb"),
     babl_component ("Cr"),
     NULL);
index faf28ecf8892bd30f964486d63b26008e6371eec..a9d163d24e1ed60b3dcbf66cd69fea4c5af61b93 100644 (file)
@@ -2017,10 +2017,13 @@ init (void)
     NULL);
   Babl *yuv8 = babl_format_new (
     "name", "Y'CbCr u8",
+    "planar",
     babl_model ("Y'CbCr"),
     babl_type ("u8-luma"),
+    babl_sampling (1, 1),
     babl_component ("Y'"),
     babl_type ("u8-chroma"),
+    babl_sampling (2, 2),
     babl_component ("Cb"),
     babl_component ("Cr"),
     NULL);